package com.hooda.utils;

import com.alibaba.druid.pool.DruidPooledConnection;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author zhangzp
 * @version 1.0, 2021/8/12
 */
public class ConnectionUtils {

    private static ConnectionUtils instance = new ConnectionUtils();

    private ConnectionUtils() {

    }

    public static ConnectionUtils getInstance() {
        return instance;
    }

    private ThreadLocal<Connection> connectionThreadLocal = new ThreadLocal<>();

    public Connection getCurrentThreadConnection() throws SQLException {
        Connection connection = connectionThreadLocal.get();
        if (connection != null) {
            return connection;
        }
        connection = DruidUtils.getInstance().getConnection();
        connectionThreadLocal.set(connection);
        return connection;
    }
}
